/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

/**
 * Reflection
 *
 * @since 2021-06-08
 */
public class Reflection {
    /**
     * injectForField
     *
     * @param obj Object
     * @param name String
     * @param value Object
     */
    public static void injectForField(Object obj, String name, Object value) {
        try {
            Field field = obj.getClass().getDeclaredField(name);
            field.setAccessible(true);
            field.set(obj, value);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * callMethod
     *
     * @param obj Object
     * @param name String
     * @param parameterType Class
     * @param value Object
     * @return Object
     * @throws NoSuchMethodException
     */
    public static Object callMethod(Object obj, String name, Class parameterType, Object value) throws NoSuchMethodException {
        Object invokeObj = null;
        try {
            invokeObj = obj.getClass().getDeclaredMethod(name, parameterType).invoke(obj, value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            Arrays.stream(obj.getClass().getInterfaces()).filter(aClass -> containsMethod(aClass, name, parameterType)).findFirst().get().getDeclaredMethod(name, parameterType);
        }
        return invokeObj;
    }

    /**
     * callMethod
     *
     * @param obj Object
     * @param name String
     * @param value Object[]
     * @param parameterTypes Class...
     * @return Object
     * @throws NoSuchMethodException
     */
    public static Object callMethod(Object obj, String name, Object[] value, Class... parameterTypes) throws NoSuchMethodException {
        Object invokeObj = null;
        try {
            invokeObj = obj.getClass().getDeclaredMethod(name, parameterTypes).invoke(obj, value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            Arrays.stream(obj.getClass().getInterfaces()).filter(aClass -> containsMethod(aClass, name, parameterTypes)).findFirst().get().getDeclaredMethod(name, parameterTypes);
        }
        return invokeObj;
    }

    private static boolean containsMethod(Class clazz, String name, Class<?>... parameterTypes) {
        try {
            clazz.getDeclaredMethod(name, parameterTypes);
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    }

    private static boolean containsMethod(Class clazz, String name, Class<?> parameterType) {
        return containsMethod(clazz, name, parameterType);
    }

    /**
     * getProperty
     *
     * @param obj Object
     * @param name String
     * @return Object
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static Object getProperty(Object obj, String name) throws NoSuchFieldException, IllegalAccessException {
        Object result = null;
        try {
            Field field = obj.getClass().getDeclaredField(name);
            field.setAccessible(true);
            result = field.get(obj);
        } catch (NoSuchFieldException e) {
            Field field = obj.getClass().getSuperclass().getDeclaredField(name);
            field.setAccessible(true);
            result = field.get(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * getCtor
     *
     * @param className String
     * @param ctorTypes Class<?>
     * @return Constructor<?>
     */
    public static Constructor<?> getCtor(String className, Class<?>... ctorTypes) {
        Constructor<?> constructor = null;
        try {
            Class<?> clazz = Class.forName(className);
            constructor = clazz.getConstructor(ctorTypes);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return constructor;
    }

}
